package com.fm.cotroller;

import com.fm.common.Const;
import com.fm.common.ResultResponse;
import com.fm.pojo.Message;
import com.fm.pojo.User;
import com.fm.service.IMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("message")
public class MessageController {

    @Autowired
    private IMessageService messageService;

    //查看留言分页
    @RequestMapping("list")
    @ResponseBody
    public ResultResponse messageList(HttpSession session,@RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                            @RequestParam(value = "pageSize",defaultValue = "10")int pageSize){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user!=null){
            return messageService.messageList(user.getId(),pageNum,pageSize);
        }
        return ResultResponse.isErrorMessage("用户未登录");
    }

    //查看留言
    @RequestMapping("detail")
    @ResponseBody
    public ResultResponse detail(HttpSession session, String messageId){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user!=null){
            return messageService.getMessage(messageId);
        }
        return ResultResponse.isErrorMessage("用户未登录");
    }

    //删除留言
    @RequestMapping(value = "delMessage",method = RequestMethod.DELETE)
    public ResultResponse<String> delMessage(HttpSession session, String messageId){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user!=null){
            return messageService.delMessage(messageId);
        }
        return ResultResponse.isErrorMessage("用户未登录");
    }

    //添加留言
    @RequestMapping(value = "addMessage",method = RequestMethod.POST)
    public ResultResponse<String> addMessage(HttpSession session, Message message){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user!=null){
            return messageService.addMessage(message);
        }
        return ResultResponse.isErrorMessage("用户未登录");
    }
}
